<?php
namespace Home\Controller;
use Think\Controller;
use Home\Biz\OrderBiz;
class IndexController extends Controller {
    public function index(){
		//验证当前用户是否登陆
		$user_id = cookie('login_user_id');
		if(empty($user_id)){
			//未登录
			redirect(C('AdminConfig.login_page'));
			return;
		}else{
			redirect(C('OrderConfig.orderinfo_page'));
		}
    }
    
    
    public function test1(){
//     	$sku_config = explode('|', C('SkuIdConfig.3116776067094'));
// //     	echo json_encode($sku_config);
//     	echo $sku_config[0].'-';
//     	echo $sku_config[1];
    	$this->InvoiceTest();
    }
    
    
    public function test(){
    	$test_array = array();
    	
    	$arr1 = array(
    			'id' => 1,
    			'name' => 'name1',
    	);
    	$arr2 = array(
    			'id' => 2,
    			'name' => 'name2',
    	);
    	$arr3 = array(
    			'id' => 3,
    			'name' => 'name3',
    	);
    	$arr4 = array(
    			'id' => 4,
    			'name' => 'name4',
    	);
    	
    	array_push($test_array, $arr1);
    	array_push($test_array, $arr2);
    	array_push($test_array, $arr3);
    	array_push($test_array, $arr4);
    	
    	foreach($test_array as $key=>$en_arr){
    		if($en_arr['id'] == 3){
    			$en_arr['name'] = 'hhhhhh';
    			$test_array[$key]['name'] = 'hhhhhh';
    		}
    	}
    	
    	echo json_encode($test_array);
    	
    	
    	
//     	$table_exchange=array(1,2,3,4,5,6,7,8);
    	
//     	foreach ($table_exchange as $b=>$c){
    	
//     		$c="Week_".$c;
    	
//     	}
    	
//     	echo $table_exchange[0];
    }
    
    
    
    public function InvoiceTest(){
    	
    	$third_order_id = '1421446988817746';
    	$invoice_serial_no = 'IV_'.date("y-m-d_H:i:s", time());
    	
    	$order_en = M('orderInfo')->where("third_order_id='$third_order_id'")->find();
    	$lst_order_detail_en = M('orderDetail')->where("third_order_id='$third_order_id'")->select();
    	
    	
    	$lst_add_invoice_item_en = $this->getOrderDetailInvoiceIntmList($invoice_serial_no, $order_en, $lst_order_detail_en);
    	
    	/**
    	 * 发票折扣行逻辑 Begin
    	 */
    	//判断是否存在折扣【订单所对应的所有order_discount(discount_type 为2和3)的折扣金额总计】
    	$discount_amount = M('orderDiscount')
	    	->where("third_order_id='$third_order_id' AND discount_type IN (2,3)")
	    	->sum('discount_amount');
    	if($discount_amount > 0){
    		//存在折扣，invoice新增折扣行，并将折扣算在最贵商品之上，更改其row_type
    		$lst_add_invoice_item_en = $this->RebuildInvoiceItemList($lst_add_invoice_item_en);
    		$max_invoice_item_en = $this->getMaxInvoiceAmountForInvoiceItemList($lst_add_invoice_item_en) ;
    		
    		//新增折扣行
    		
    		//折扣税率
    		$discount_tax_rate = $max_invoice_item_en['tax_rate'];
    		
    		$invoice_amount = $discount_amount;
    		$tax 			= $discount_amount * $discount_tax_rate;
    		$sum_price		= $invoice_amount - $tax;
    		
    		
    		$discount_invoice_item_en = array();
    		$discount_invoice_item_en['invoice_serial_no']	= $invoice_serial_no;
    		$discount_invoice_item_en['order_id']			= $order_en['order_id'];
    		$discount_invoice_item_en['third_order_id']	= $order_en['third_order_id'];
    		$discount_invoice_item_en['item_name']			= '折扣';
//     		$discount_invoice_item_en['price']				= $discount_price;
    		$discount_invoice_item_en['sum_price']			= $sum_price;
    		$discount_invoice_item_en['tax_rate']			= $discount_tax_rate;
    		$discount_invoice_item_en['tax']				= $tax;
    		$discount_invoice_item_en['invoice_amount']		= $invoice_amount;
//     		$discount_invoice_item_en['quantity']			= $order_detail_en['qty'];
//     		$discount_invoice_item_en['specification']		= $sku_config[3];
//     		$discount_invoice_item_en['unit']				= $sku_config[1];
    		$discount_invoice_item_en['row_type']			= '1';
    		
    		array_push($lst_add_invoice_item_en, $discount_invoice_item_en);
    	}
    	
    	/**
    	 * 发票折扣行逻辑 End
    	 */
    	
    	
    	//写入invoice_item
    	foreach($lst_add_invoice_item_en as $add_invoice_item_en){
    		M('orderInvoiceItem')->add($add_invoice_item_en);
    	}
    	
    	
    }
    
    /**
     * 重构invoice_item集合
     * 将折扣算在最贵商品之上，更改其row_type，并返回最新的invoice_item集合
     */
    public function RebuildInvoiceItemList($lst_invoice_item_en){
    	
    	$key = '';
    	$max_amount = 0;

    	foreach($lst_invoice_item_en as $key=>$invoice_item_en){
    		if($invoice_item_en['invoice_amount'] > $max_amount){
    			$max_amount = $invoice_item_en['invoice_amount'];
    			$key = $key;
    		}
    	}
    	
    	$lst_invoice_item_en[$key]['row_type'] = '2';
    	
    	return $lst_invoice_item_en;
    }
    
    
    /**
     * 获得最贵的invoice_item
     */
    public function getMaxInvoiceAmountForInvoiceItemList($lst_invoice_item_en){
    	 
    	$key = '';
    	$max_amount = 0;
    	    	foreach($lst_invoice_item_en as $key=>$invoice_item_en){
    		if($invoice_item_en['invoice_amount'] > $max_amount){
    			$max_amount = $invoice_item_en['invoice_amount'];
    			$key = $key;
    		}
    	}
    	 
    	return $lst_invoice_item_en[$key];
    }
    
    
    public function getOrderDetailInvoiceIntmList($invoice_serial_no, $order_en, $lst_order_detail_en){
    	$lst_HasServerPackSkUId = explode(',', C('InvoiceConfig.HasServerPackSkUIdList'));
    	$lst_add_invoice_item_en = array();
    	foreach($lst_order_detail_en as $order_detail_en){
    	
    		//是否存在于服务包价格
    		if(in_array($order_detail_en['third_sku_id'], $lst_HasServerPackSkUId)){
    			$sku_config = explode('|', C('SkuIdConfig.'.$order_detail_en['third_sku_id']));
    			 
    			/**
    			 * 电视机invoice_item Begin
    			 */
    			//电视机税率
    			$goods_tax_rate = $sku_config[2];
    			//商品含税单价(detail中的单价减去配置中的服务包价格)
    			$goods_price	= $order_detail_en['sale_price'] - $sku_config[4];
    			 
    			$invoice_amount = $goods_price * $order_detail_en['qty'];
    			$tax 			= $invoice_amount - $invoice_amount / (1 + $goods_tax_rate);
    			$sum_price		= $invoice_amount - $tax;
    			 
    			 
    			$goods_invoice_item_en = array();
    			$goods_invoice_item_en['invoice_serial_no']	= $invoice_serial_no;
    			$goods_invoice_item_en['order_id']			= $order_en['order_id'];
    			$goods_invoice_item_en['third_order_id']	= $order_en['third_order_id'];
    			$goods_invoice_item_en['item_name']			= $sku_config[0];
    			$goods_invoice_item_en['price']				= $goods_price;
    			$goods_invoice_item_en['sum_price']			= $sum_price;
    			$goods_invoice_item_en['tax_rate']			= $goods_tax_rate;
    			$goods_invoice_item_en['tax']				= $tax;
    			$goods_invoice_item_en['invoice_amount']	= $invoice_amount;
    			$goods_invoice_item_en['quantity']			= $order_detail_en['qty'];
    			$goods_invoice_item_en['specification']		= $sku_config[3];
    			$goods_invoice_item_en['unit']				= $sku_config[1];
    			$goods_invoice_item_en['row_type']			= '0';
    			 
    			array_push($lst_add_invoice_item_en, $goods_invoice_item_en);
    			/**
    			 * 电视机invoice_item End
    			 */
    			 
    			   
    			/**
    			 * 服务包invoice_item Begin
    			 */
    			$server_tax_rate = $sku_config[2];
    			//商品含税单价(detail中的单价减去配置中的服务包价格)
    			$server_price	= $sku_config[4];
    	
    			$invoice_amount = $server_price * $order_detail_en['qty'];
    			$tax 			= $invoice_amount / (1 + $server_tax_rate);
    			$sum_price		= $invoice_amount - $tax;
    	
    	
    			$server_invoice_item_en = array();
    			$server_invoice_item_en['invoice_serial_no']= $invoice_serial_no;
    			$server_invoice_item_en['order_id']			= $order_en['order_id'];
    			$server_invoice_item_en['third_order_id']	= $order_en['third_order_id'];
    			$server_invoice_item_en['item_name']		= $sku_config[5];
    			$server_invoice_item_en['price']			= $server_price;
    			$server_invoice_item_en['sum_price']		= $sum_price;
    			$server_invoice_item_en['tax_rate']			= $server_tax_rate;
    			$server_invoice_item_en['tax']				= $tax;
    			$server_invoice_item_en['invoice_amount']	= $invoice_amount;
    			$server_invoice_item_en['quantity']			= $order_detail_en['qty'];
    			$server_invoice_item_en['specification']	= $sku_config[6];
    			$server_invoice_item_en['unit']				= '';
    			$server_invoice_item_en['row_type']			= '0';
    	
    			array_push($lst_add_invoice_item_en, $server_invoice_item_en);
    			/**
    			 * 服务包invoice_item End
    			 */
    			 
    		}
    		else{
    			/**
    			 * 普通商品invoice_item Begin
    			 */
    			//普通商品税率
    			$goods_tax_rate = C('InvoiceConfig.DefaultTaxRate');
    			//商品含税单价(detail中的单价减去配置中的服务包价格)
    			$goods_price	= $order_detail_en['sale_price'];
    	
    			$invoice_amount = $goods_price * $order_detail_en['qty'];
    			$tax 			= $invoice_amount / (1 + $goods_tax_rate);
    			$sum_price		= $invoice_amount - $tax;
    	
    	
    			$goods_invoice_item_en = array();
    			$goods_invoice_item_en['invoice_serial_no']	= $invoice_serial_no;
    			$goods_invoice_item_en['order_id']			= $order_en['order_id'];
    			$goods_invoice_item_en['third_order_id']	= $order_en['third_order_id'];
    			$goods_invoice_item_en['item_name']			= $sku_config[0];
    			$goods_invoice_item_en['price']				= $goods_price;
    			$goods_invoice_item_en['sum_price']			= $sum_price;
    			$goods_invoice_item_en['tax_rate']			= $goods_tax_rate;
    			$goods_invoice_item_en['tax']				= $tax;
    			$goods_invoice_item_en['invoice_amount']	= $invoice_amount;
    			$goods_invoice_item_en['quantity']			= $order_detail_en['qty'];
    			$goods_invoice_item_en['specification']		= '';
    			$goods_invoice_item_en['unit']				= '';
    			$goods_invoice_item_en['row_type']			= '0';
    	
    			array_push($lst_add_invoice_item_en, $goods_invoice_item_en);
    			/**
    			 * 电视机invoice_item End
    			 */
    		}
    	}
    	
    	return $lst_add_invoice_item_en;
    }
    
    
    
}